import 'package:flutter/material.dart';
import 'package:flutter_application_1/pages/my/collect.dart';
import 'package:flutter_application_1/repository/dto/auth_dto.dart';

import '../../constants.dart';
import '../../utils/sp_utils.dart';
import '../auth/auth_vm.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  late int cookieLen = SpUtils.getStringList(Constants.COOKIE_LIST).length;
  late String username = SpUtils.getString(Constants.USERNAME);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            _handler((){
              if(cookieLen == 0) {
                Navigator.pushNamed(context, '/login');    
              }
            }),
            _item('我的收藏', onTap: () async {
                Navigator.push(context, MaterialPageRoute(builder: (context) => CollectPage()));
            }),
            _item('版本检查'),
            _item('关于我们'),
            _item('退出登录', onTap: () async {
              await AuthViewModel().logout();
              Navigator.pushReplacementNamed(context, '/login');
            }),
          ],
        ),
      ),
    );
  }

  Widget _item(String title, {VoidCallback? onTap}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey[300]!),
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: Row(
          children: [
            Text(title,style: TextStyle(fontSize: 16.0,fontWeight: FontWeight.bold),),
            Spacer(),
            Icon(Icons.arrow_forward_ios,size: 16.0,),
          ],
        ),
      ),
    );
  }

  Widget _handler(onTap) {
    return Container(
        width: double.infinity,
        height: 300,
        padding: const EdgeInsets.all(16.0),
        color: Colors.amber,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: onTap,
              child: CircleAvatar(
                radius: 55.0,
                backgroundImage: AssetImage('images/a.jpeg'),
              ),
            ),
            SizedBox(height: 16.0),
            GestureDetector(
              onTap: onTap,
              child: Text(username,style: TextStyle(fontSize: 24.0,fontWeight: FontWeight.bold, color: Colors.white),),
            ),
          ],
        ),
      );
  }

}
